//
//  BSEssenceViewController.m
//  01-BuDeJie
//
//  Created by 1 on 15/12/17.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "BSEssenceViewController.h"
#import "BSTitleButton.h"

#import "BSAllViewController.h"
#import "BSPictureViewController.h"
#import "BSVideoViewController.h"
#import "BSWordViewController.h"
#import "BSMusicViewController.h"

#import "BSTopicTableViewController.h"

@interface BSEssenceViewController ()<UIScrollViewDelegate>
/** titleView*/
@property (nonatomic ,weak) UIView *titleView;
/** clickedTitleButton*/
@property (nonatomic ,weak) BSTitleButton *clickedTitleButton;
/** underlineView*/
@property (nonatomic ,weak) UIView *underlineView;
/** scrollView*/
@property (nonatomic ,weak) UIScrollView *scrollView;
@end

@implementation BSEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BSCommonBgColor;
    
    [self setUpBarButtonItem];
    
    [self setUpChildViewController];
    
    //设置scrollView
    [self setUpScrollView];
    
    //设置标题
    
    [self setUpTitleView];
    
    //将第一个子控件 VIew加到父控制器中
    [self setUpChildViewContrillerViewInToScrollView];

    }
-(void)setUpChildViewController{
    //全部
    BSAllViewController *allView = [[BSAllViewController alloc]init];
    [self addChildViewController:allView];
    //视屏
    BSVideoViewController *videoView = [[BSVideoViewController alloc]init];
    [self addChildViewController:videoView];
    //图片
    BSPictureViewController * picView = [[BSPictureViewController alloc]init];
    [self addChildViewController:picView];
    //音乐
    BSMusicViewController *music = [[BSMusicViewController alloc]init];
    [self addChildViewController:music];
    //文字
    BSWordViewController *wordView = [[BSWordViewController alloc]init];
    [self addChildViewController:wordView];
    
}
-(void)setUpScrollView{
    // 不要自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
//    scrollView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:scrollView];
    
     // 添加子控制器的view到scrollView中
    NSInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * BSScreenW, BSScreenH);
    scrollView.pagingEnabled = YES;
    self.scrollView = scrollView;
    
    scrollView.delegate = self;
    
//    for (NSUInteger i = 0; i < count; i++) {
//        // 取出i位置对应的子控制器
//        UIViewController *childVc = self.childViewControllers[i];
//        
//        // 添加子控制器的view
//        [scrollView addSubview:childVc.view];
//        
//        // 设置frame
//        childVc.view.x = i * scrollView.width;
//        childVc.view.y = 0;
//        childVc.view.height = scrollView.height;
//    }

}

-(void)setUpTitleView{
    
    UIView *titleView = [[UIView alloc]init];
    titleView.frame = CGRectMake(0, BSNavMaxY, BSScreenW, BSTitlesViewH);
    
    [self.view addSubview:titleView];
    
    self.titleView = titleView;
    //设置 titleView 的背景色
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];

    
    //设置子控制器
    [self setUpTitleButton];
    
    //设置底部条
    [self setUpUnderline];

}

//设置 titleButton
-(void)setUpTitleButton{
    
    NSArray *titlesArray = @[@"全部",@"视频",@"图片",@"音乐",@"段子"];
    
    NSInteger count = titlesArray.count;
    CGFloat buttonW = BSScreenW/count;
    CGFloat buttonH = self.titleView.height;
    
    for (NSInteger i = 0; i < count; i++) {
        BSTitleButton *button = [BSTitleButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*buttonW, 0, buttonW, buttonH);
//        button.backgroundColor = [UIColor greenColor];
        //button 设置 title 要份状态来
        [button setTitle:titlesArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchDown];
        [self.titleView addSubview:button];
        
        button.tag = i;
    }

}
/**
 * 添加底部的下划线
 */
- (void)setUpUnderline
{
    // 取出第一个标题按钮
    BSTitleButton *firstTitleButton = self.titleView.subviews.firstObject;
    
    // 标题栏
    UIView *underlineView = [[UIView alloc] init];
    underlineView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    
    underlineView.height = 2;
    underlineView.y = self.titleView.height - underlineView.height;
    [self.titleView addSubview:underlineView];
    
    // 默认选中第一个按钮
    // 切换按钮状态
    firstTitleButton.selected = YES; // 新点击的按钮
    self.clickedTitleButton = firstTitleButton;
    // 下划线的宽度 == 按钮文字的宽度
    //一开始执行的时候,还没有来得及算尺寸,通过 sizeToFit 来计算尺寸
    [firstTitleButton.titleLabel sizeToFit]; // 通过这句代码计算按钮内部label的宽度
    underlineView.width = firstTitleButton.titleLabel.width + BSMargin;
    // 下划线的位置
    underlineView.centerX = firstTitleButton.centerX;
    
    self.underlineView = underlineView;
}


-(void)setUpBarButtonItem{
    
    //设置左边的UIBarButtonItem
    UIBarButtonItem *leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_item_game_icon"] HighLightImage:[UIImage imageNamed:@"nav_item_game_click_icon"] Target:self Action:@selector(leftButtonClick)];
    
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
     //设置右边的UIBarButtonItem
    
    UIBarButtonItem *rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationButtonRandom"] HighLightImage:[UIImage imageNamed:@"navigationButtonRandomClick"] Target:self Action:@selector(rightButtonClick)];
    
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    //设置中间的标题
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
}


#pragma mark ---------------
#pragma mark - 左右边点击
-(void)leftButtonClick{
    BSFunc;
}
-(void)rightButtonClick{
    BSFunc;
}

-(void)titleButtonClick:(BSTitleButton *)button{
    
   //获取以前的TableView
    BSTopicTableViewController *vc = self.childViewControllers[self.clickedTitleButton.tag];
    [vc setUpPlayView];
    
    
//    NSLog(@"%@", self.clickedTitleButton);
    self.clickedTitleButton.selected = NO;
    button.selected = YES;
    self.clickedTitleButton = button;
    
//    NSLog(@"%@", self.clickedTitleButton);
    
    [UIView animateWithDuration:0.25 animations:^{
        
        //设置选中的条
        // 下划线的宽度 == 按钮文字的宽度
        self.underlineView.width = button.titleLabel.width + BSMargin;
        self.underlineView.centerX = self.clickedTitleButton.centerX;
        
        //设置选中的控制器--控制偏移量
        NSUInteger index = button.tag;
        // 让scrollView滚动到对应位置
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = index * self.scrollView.width; // 只修改x值,不要去修改y值
        self.scrollView.contentOffset = offset;
    }completion:^(BOOL finished) {
        [self setUpChildViewContrillerViewInToScrollView];
    }];
}

-(void)setUpChildViewContrillerViewInToScrollView{
    
    //chlidVC的 view 的 x 等于偏移量
    
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.width;
    
    BSTopicTableViewController *vc = self.childViewControllers[index];
    vc.view.x = self.scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.width = self.scrollView.width;
    vc.view.height = self.scrollView.height;
    
    [self.scrollView addSubview:vc.view];
    
}
#pragma mark ---------------
#pragma mark - scrollviewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  
    NSInteger  index= self.scrollView.contentOffset.x / self.scrollView.width;
    
    //设置相应的 button---使用 tag 来查找, tag = 0会出现直接使用父类的 titleView 情况
    
    BSTitleButton * button = self.titleView.subviews[index];
    [self titleButtonClick:button];
    
    //    BSTitleButton *titleButton = [self.titlesView viewWithTag:index];
   
}

@end
